$(document).ready(function(){
	
	$('.fire-search').click(function(){
		var txt = "";
	
		$('.modal-win-start').click(function(){
			var query=$('#txt').val();
			var searchBtn=$('#search');
			var chooseBtn=$('#choose');
			var id;
			var name;
			var type;
			$.ajax({
		        type: "POST",
		        url: "searchBoth",
		        data: "query="+query,
		        success: function(response){
		        	$('.searchBothResult').remove();
		        	$('.error-block').remove();
		        	if (response.toLowerCase().indexOf("searchbothresultrow") >= 0){
		        	chooseBtn.before(response);
		        	 $('input[name=result]:radio').change(function(){
		        		 
		        		 id=$(this).val();
		        		 name=$(this).parent().find('.searchBothResultName').html();
		        		 type =$(this).parent().find('.searchBothResultType').html();
		        		 chooseBtn.attr("class","form-btn");
		        	 });
		        	 $('#choose').click(function(){
		        		 $('#parentId').attr('value',id);
		        		 $('#parentName').attr('value',name);
		        		 $('#type').attr('value',type);
		        		 
		        		 $(this).modalWinHide();
		        		 
		        	 });
		        	}
		        	else{
		        		chooseBtn.before('<div class="error-block">No search result for "' + query + '"</div>');
		        	}
		        },
		        error: function(e){
		        	$('.searchBothResult').remove();
		        	$('.error-block').remove();
	        		chooseBtn.before('<div class="error-block">No search result for "' + query + '"</div>');
		        }
		        });
		});
		
	    $(this).modalWinShow({txt: txt});
	    });
	
	
/************************************************************/
	

	
	$('.fire-search-sku').click(function(){
		txtIn = $(this);
		txtInId = $(this).attr("id");
		var txt = "";
		$("#searchcontact").css("display","none");
		$("#searchSkuMod").css("display","inline-table");
    	$('.searchBothResult').remove();
		$('#searchSkuMod').click(function(){
			var query=$('#txt').val();
			var searchBtn=$('#search');
			var chooseBtn=$('#choose');
			var id;
			var name;
			var type;
			
			$.ajax({
				
		        type: "POST",
		        url: "searchSku",
		        data: "query="+query,
		        success: function(response){
		        	$('.searchBothResult').remove();
		        	$('.error-block').remove();
		        	if (response.toLowerCase().indexOf("searchbothresultrow") >= 0){
		        	chooseBtn.before(response);
		        	 $('input[name=result]:radio').change(function(){
		        		 id=$(this).val();
		        		 name=$(this).parent().find('.searchBothResultName').html();
		        		 code=$(this).parent().find('.searchBothResultCode').html();
		        		 price=$(this).parent().find('.searchBothResultPrice').html();
		        		 skuProdId=$(this).parent().find('.searchBothResultId').html();
		        		 chooseBtn.attr("class","form-btn");
		        	 });
		        	 $('#choose').click(function(){
		        		 $("#" + txtInId).attr('value',name);
		        		 $("#skuName" + txtInId).attr('value',code);
		        		 $("#skuProdId" + txtInId).attr('value',skuProdId);
		        		 $("#price" + txtInId).attr('value',price);
		        		 txtIn.attr('value',name);
		        		 $(this).modalWinHide();
		        	 });
		        	}
		        	else{
		        		chooseBtn.before('<div class="error-block">No search result for "' + query + '"</div>');
		        	}
		        },
		        error: function(e){
		        	$('.searchBothResult').remove();
		        	$('.error-block').remove();
	        		chooseBtn.before('<div class="error-block">No search result for "' + query + '"</div>');
		        }
		        });
		});
		
	    $(this).modalWinShow({txt: txt});
	    });	
	$('.fire-contact-search').click(function(){
		txtIn = $(this);
		txtInId = $(this).attr("id");
		//id: contactedPerson
		$("#searchcontact").css("display","inline-table");
		$("#searchSkuMod").css("display","none");
    	$('.searchBothResult').remove();
		var txt = "";
		$('#searchcontact').click(function(){
			var query=$('#txt').val();
			var searchBtn=$('#search');
			var chooseBtn=$('#choose');
			var id;
			var fName;
			var lName;
			var contactType;
			
			$.ajax({
				
		        type: "POST",
		        url: "searchContacts",
		        data: "query="+query,
	        	//insert in new-invoice ".searchBothResult"
	        	//$('.error-block').remove();
		        success: function(response){
		        	$('.searchBothResult').remove();
		        	$('.error-block').remove();
		        	if (response.toLowerCase().indexOf("searchbothresultrow") >= 0){
		        	chooseBtn.before(response);
		        	 $('input[name=result]:radio').change(function(){
		        		 id=$(this).val();
		        		 fName=$(this).parent().find('.searchBothResultFName').html();
		        		 lName=$(this).parent().find('.searchBothResultLName').html();
		        		 contactType=$(this).parent().find('.searchBothResultType').html();
		        		 chooseBtn.attr("class","form-btn");
		        	 });
		        	 $('#choose').click(function(){
		        		 //alert("contact fname, lname" + fName + " " + lName );
		        		 $("#" + txtInId).attr('value', fName + " " + lName + " (" + contactType + ")" );
		        		 //txtIn.attr('value',name);
		        		 $("#contactedPersonId").attr("value", id);
		        		 $(this).modalWinHide();
		        	 });
		        	}
		        	else{
		        		chooseBtn.before('<div class="error-block">No search result for "' + query + '"</div>');
		        	}
		        },
		        error: function(e){
		        	$('.searchBothResult').remove();
		        	$('.error-block').remove();
	        		chooseBtn.before('<div class="error-block">No search result for "' + query + '"</div>');
		        }
		        });
		});
		
	    $(this).modalWinShow({txt: txt});
	    });
	
	
	
	
	$('.fire-lead').click(function(){
		var txt = "<h5>How to create a lead:</h5> <ol><li>Select the Leads tab,</li> <li>then click New In the lead form,</li> <li>enter all of the information that you have for this lead, including contact information, lead source, industry, and any notes If desired, <li>you can also rate the lead and qualify it from this page.</li> <li>When you are done, click Save</li>";
		$(".modal-win-start").attr("href", "newLead.html");
	    $(this).modalWinShow({txt: txt});
	    });
	$('.fire-account').click(function(){
		var txt = "<h5>How to create an account:</h5> <ol><li>Select the accounts tab,</li> <li>then click New In the account form,</li> <li>enter all of the information that you have for this account, including contact information, lead source, industry, and any notes If desired, <li>you can also rate the account and qualify it from this page.</li> <li>When you are done, click Save</li>";
		$(".modal-win-start").attr("href", "new-account.html");
	    $(this).modalWinShow({txt: txt});
	    });
	$('.fire-contact').click(function(){
		var txt = "<h5>How to create a contact:</h5> <ol><li>Select the contacts tab,</li> <li>then click New In the contact form,</li> <li>enter all of the information that you have for this contact, including contact information, lead source, industry, and any notes If desired, <li>When you are done, click Save</li>";
		$(".modal-win-start").attr("href", "new-contact.html");
	    $(this).modalWinShow({txt: txt});
	    });
	$('.delete-lead').click(function(){
		var txt = "<h5>Do you Want to Delete that Lead?:</h5>";
		var id = tID;
		$(".modal-win-start").attr("href", "/sales/s/deleteLead?id="+id);
	    $(this).modalWinShow({txt: txt});
	    });
	$('.modal-win-close').click(function(){
	    $(this).modalWinHide();
	    });
	$('.modal-win-ok').click(function(){
	    $(this).modalWinHide();
	    });
	
	
	$.fn.modalWinShow = function() {
     var args = arguments[0] || {};
     var txt = args.txt;
	    $('.modal-win-title').text($(this).text()) ;
	    $('.modal-win-content').html(txt);
	    $('.modal-win-bg').css('display', 'block');
	    $('.modal-win-bg').animate({opacity: 1}, 1000);
	 }
	 
	 
	 $.fn.modalWinHide = function() {
	    $('.modal-win-bg').animate({ opacity: 0 }, 500, function(){ $(this).css('display', 'none'); });
	 }
	 
});



